local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local tabletool = require"tabletool"
local timetool = require "timetool"
require "pszenum"
local tablelogic = require("tablelogic")

local TableTimer = {}

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]
	if seat.rid ~= request.rid or seat.ready_timer_id ~= timerid then
		return
	end
	seat.ready_timer_id = -1

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
		-----判断朋友桌局数到没有
		if seat then
			if table_data.cur_games_times < table_data.conf.game_times then
				-- seat.state = ESeatState.SEAT_STATE_WAIT_START
				-- tablelogic.gameready(table_data, request, seat)
				return
			end
			tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	--[[
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_DIAMOND then
		if seat then
			if table_data.cur_games_times < table_data.conf.game_times then
				-- seat.state = ESeatState.SEAT_STATE_WAIT_START
				-- tablelogic.gameready(table_data, request, seat)
				return
			end
			tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
		end
	--]]
	else
		tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)
	end
end

function TableTimer.doreadydelay(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]
	local outoftimermsg = {
		rid = seat.rid,
		seat_index = seat.index,
	}
	seat.ready_timer_id = timer.settimer(table_data.conf.ready_timeout*100, "doready", outoftimermsg)
	seat.ready_to_time = timetool.get_time() + table_data.conf.ready_timeout
	local doreadyntcmsg = {
		rid = seat.rid,
		seat_index = seat.index,
		ready_to_time = timetool.get_time() + table_data.conf.ready_timeout,
		is_ready = EBOOL.FALSE,
	}
	msghelper:sendmsg_to_alltableplayer("doreadyN", doreadyntcmsg)
end

function TableTimer.onegamestart(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
	table_data.timer_id = -1
	server.logic.gamelogic.onegamestart()
end

function TableTimer.todonotice(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.todo_time_id ~= timerid then
		return
	end
	table_data.todo_time_id = -1

	msghelper:sendmsg_to_alltableplayer("todoN", request)
end

function TableTimer.doaction(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]
	
	if table_data.timer_id ~= timerid then
		filelog.sys_error("游戏在doaction后timerid变了......")
		return
	end 
	table_data.timer_id = -1

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_CLIENT_ACTION 
	or table_data.seat_index ~= request.seat_index
	or request.rid ~= seat.rid then
		return
	end

	if tablelogic.is_onegameend(table_data) then
		return
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_ZJ_FRIEND then
		return
	end

	local actrequest = {
		action_type = EActionType.ACTION_TYPE_FOLD,
		rid = seat.rid,
	}
	tablelogic.doaction(table_data, actrequest, seat)

	if seat.is_tuoguan == EBOOL.TRUE and seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
		tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_TIMEOUT_STANDUP)
		return
	end
end

function TableTimer.forcecompare(timerid)
	local server = msghelper:get_server()
	local table_data = server.table_data
	
	if table_data.timer_id ~= timerid then return end 
	table_data.timer_id = -1

	server.logic.gamelogic.forcecompare()
end

function TableTimer.delete_table(timerid, request)
    local server = msghelper:get_server()    
    local table_data = server.table_data

	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats or {}) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
        table_data.delete_table_timer_id = -1
        server.delete()
    end 
end

function TableTimer.onegamerealend(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then   --timer_id 改变，所以不能进入gamefnlogic.onegamerealend
		filelog.sys_error("游戏结束在onegamerealend后timerid变了......", timerid, table_data.timer_id)
		-- return
	end

	table_data.timer_id = -1
	server.logic.gamelogic.onegamerealend()
end

function TableTimer.doreadynotice(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then   --timer_id 改变，所以不能进入gamefnlogic.onegamerealend
		filelog.sys_error("timerid变了......")
		return
	end

	table_data.timer_id = -1
	server.logic.gamelogic.doreadynotice()
end

function TableTimer.disbandtable(timerid)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.vote_timerid ~= timerid then
		return
	end
	-- local noteresultntcmsg = {
	-- 	result = 0,
	-- 	names = table_data.vote_names,
	-- }
	local noteresultntcmsg = {
		table_index = table_data.conf.idx,
		is_success = false,
	}
	if not table_data.vote_num or table_data.vote_num == 0 then
		noteresultntcmsg.is_success = true
	end
	msghelper:sendmsg_to_alltableplayer("disbandresultN", noteresultntcmsg)
	table_data.vote_timerid = nil
	table_data.vote_rid = nil
	table_data.vote_name = nil
	table_data.vote_timeout = nil
	table_data.vote_num = nil
	table_data.vote_answers = nil
	-- table_data.vote_names = nil

	if noteresultntcmsg.is_success then
		if table_data.timer_id and table_data.timer_id > 0 then
			timer.cleartimer(table_data.timer_id)
			table_data.timer_id = -1
		end
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				server.logic.tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
			end
		end
		-- server.delete(table_data)
		table_data.delete_table_timer_id = timer.settimer(0, "delete_table")
	end
end

return TableTimer